#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class TrGroupMemberProvider : CY.GFive.Core.Providers.DALProvider.ITrGroupMemberProvider
	{
		#region Sql
		private static readonly string SqlSelectTrGroupMember = "SELECT [Id], [TrGroup],[TeacherCode],[JoinDate],[PositionTitle] FROM [TrGroupMember] WHERE  [Id]=@Id";

		private static readonly string SqlInsertTrGroupMember = "INSERT INTO [TrGroupMember]([TrGroup],[TeacherCode],[JoinDate],[PositionTitle]) values(@TrGroup,@TeacherCode,@JoinDate,@PositionTitle);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateTrGroupMember = "UPDATE [TrGroupMember] SET [TrGroup] = @TrGroup,[TeacherCode] = @TeacherCode,[JoinDate] = @JoinDate,[PositionTitle] = @PositionTitle WHERE  [Id]=@Id";

		private static readonly string SqlDeleteTrGroupMember = "DELETE [TrGroupMember] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllTrGroupMember = "SELECT [Id], [TrGroup],[TeacherCode],[JoinDate],[PositionTitle] FROM [TrGroupMember]";
		#endregion

		#region ITrGroupMemberProvider Members
		public Core.Business.TrGroupMember Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectTrGroupMember);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.TrGroupMember trGroupMember = new Core.Business.TrGroupMember();

				if (!reader.IsDBNull(0)) trGroupMember.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) trGroupMember.TrGroup = reader.GetString(1);
				if (!reader.IsDBNull(2)) trGroupMember.TeacherCode = reader.GetString(2);
				if (!reader.IsDBNull(3)) trGroupMember.JoinDate = reader.GetDateTime(3);
				if (!reader.IsDBNull(4)) trGroupMember.PositionTitle = reader.GetString(4);

				return trGroupMember;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.TrGroupMember trGroupMember)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@TrGroup", SqlDbType.NVarChar, trGroupMember.TrGroup);
			sql.AddParameter("@TeacherCode", SqlDbType.NVarChar, trGroupMember.TeacherCode);
			sql.AddParameter("@JoinDate", SqlDbType.DateTime, trGroupMember.JoinDate);
			sql.AddParameter("@PositionTitle", SqlDbType.NVarChar, trGroupMember.PositionTitle);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertTrGroupMember);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) trGroupMember.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.TrGroupMember trGroupMember)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@TrGroup", SqlDbType.NVarChar, trGroupMember.TrGroup);
			sql.AddParameter("@TeacherCode", SqlDbType.NVarChar, trGroupMember.TeacherCode);
			sql.AddParameter("@JoinDate", SqlDbType.DateTime, trGroupMember.JoinDate);
			sql.AddParameter("@PositionTitle", SqlDbType.NVarChar, trGroupMember.PositionTitle);
			sql.AddParameter("@Id", SqlDbType.Int, trGroupMember.Id);
			sql.ExecuteSql(SqlUpdateTrGroupMember);
		}

		public void Delete(Core.Business.TrGroupMember trGroupMember)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, trGroupMember.Id);
			sql.ExecuteSql(SqlDeleteTrGroupMember);
		}

		public IList<Core.Business.TrGroupMember> GetAllTrGroupMember()
		{
			IList<Core.Business.TrGroupMember> trGroupMemberlist = new List<Core.Business.TrGroupMember>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllTrGroupMember);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.TrGroupMember trGroupMember = new Core.Business.TrGroupMember();

					if (!reader.IsDBNull(0)) trGroupMember.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) trGroupMember.TrGroup = reader.GetString(1);
					if (!reader.IsDBNull(2)) trGroupMember.TeacherCode = reader.GetString(2);
					if (!reader.IsDBNull(3)) trGroupMember.JoinDate = reader.GetDateTime(3);
					if (!reader.IsDBNull(4)) trGroupMember.PositionTitle = reader.GetString(4);

					trGroupMember.MarkOld();
					trGroupMemberlist.Add(trGroupMember);
				}
				reader.Close();
			}
			return trGroupMemberlist;
		}
		#endregion
	}
}
